import requests
from lxml import etree
import os
# 多线程  多进程 协程
# url = "https://pixabay.com/zh/"
# https://cdn-au.onetrust.com/consent/52f0ce5e-0c2d-48a6-8e40-e978c5058a96/52f0ce5e-0c2d-48a6-8e40-e978c5058a96.json

# https://pixabay.com/bootstrap/1e73f9bc4b40eb9fe622b5a0673be65dfef6d51b30a8cb5c1d11dac397d45eb1.json

# https://pixabay.com/bootstrap/1e73f9bc4b40eb9fe622b5a0673be65dfef6d51b30a8cb5c1d11dac397d45eb1.json

# https://cdn-au.onetrust.com/consent/52f0ce5e-0c2d-48a6-8e40-e978c5058a96/52f0ce5e-0c2d-48a6-8e40-e978c5058a96.json

import re
session  = requests.session()

def getpage(page,search):
    # /images/search/美女/
    session.get("https://pixabay.com/zh")
    header = {"Accept-Language":"zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7","Referer":"https://pixabay.com/zh/","User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"}
    url =  f"https://pixabay.com/zh/images/search/{search}" if search else f"https://pixabay.com/zh/images/search/?order=ec&pagi={page}"
    res = session.get(url,headers=header)
    res.encoding = 'utf-8'
    htmlObj = etree.HTML(res.text)
    lis = htmlObj.xpath('//body/script[1]/text()')[0]
    # print(lis)
    obj = re.compile(r"window.__BOOTSTRAP_URL__ = '(?P<name>.*?)';",re.S)#re.S可以让.匹配换行
    reszj = obj.finditer(lis)
    returnUrl = ""
    for item in reszj:
        name = item.group("name").strip()
        returnUrl = name
        break
    return returnUrl
    

def getJsonInterface(path,id,search):    
    url1 = f"https://pixabay.com/{path}"
    headers = {"Referer":f"https://pixabay.com/zh/images/search/?order=ec&pagi={id}",
    "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
    }
    res =  session.get(url=url1,headers=headers)
    # res.encoding = 
    results = res.json()["page"]["results"]
    for sources in results:
        image_URL = sources["sources"]["2x"]
        image_respon = requests.get(image_URL)
        print(image_respon)
        imageName = image_URL.replace("https://cdn.pixabay.com/photo/","").replace("/","-")
        filepath = f"{search}/{imageName}" if search else f"images/{imageName}"
        # 获取文件所在文件夹的路径
        folderpath = os.path.dirname(filepath)
        print("filepath=",filepath)
        print("folderpath=",folderpath)  
        # 如果文件夹不存在，创建文件夹
        if not os.path.exists(folderpath):
            os.makedirs(folderpath)

        print(folderpath)    
        with open(filepath, 'wb') as f:#f'/Users/apple/gitee/think-for-yourself/Scrapy学习/tutorial/images/{imageName}'
            f.write(image_respon.content)
    # print(res.json()["page"]["results"])
pass    

def downLoad(index,search):
    for i in range(1,index):
        path = getpage(i,search)
        imageUrl = getJsonInterface(path=path,id=i,search=search)
    pass

# print(imageUrl)

downLoad(2,"肌肉")

